package cn.juque.jdfsystem.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.text.CharSequenceUtil;
import cn.juque.common.base.PageInfo;
import cn.juque.common.exception.AppException;
import cn.juque.jdfsystem.entity.PermissionInfo;
import cn.juque.jdfsystem.enums.JdfSystemMsgEnum;
import cn.juque.jdfsystem.service.IPermissionInfoService;
import cn.juque.lucenecandy.core.base.*;
import cn.juque.lucenecandy.core.enums.MatchTypeEnum;
import cn.juque.lucenecandy.helper.IndexHelper;
import org.apache.lucene.search.BooleanClause;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author juque
 * @version 1.0.0
 * <ul>
 *     <li>PermissionInfoServiceImpl</li>
 * </ul>
 * @date 2024-01-25
 **/
@Service("permissionInfoService")
public class PermissionInfoServiceImpl implements IPermissionInfoService {

    @Resource
    private IndexHelper indexHelper;

    /**
     * 分页查询
     *
     * @param pageInfo       分页信息
     * @param permissionInfo 参数
     * @return list
     */
    @Override
    public List<PermissionInfo> listByPage(PageInfo pageInfo, PermissionInfo permissionInfo) {
        QueryWrapperBuilder<PermissionInfo> queryWrapperBuilder = this.queryBuilder(permissionInfo);
        queryWrapperBuilder.pageInfo(pageInfo);
        return this.indexHelper.searchByPage(queryWrapperBuilder.build());
    }

    /**
     * 详情
     *
     * @param id 主键
     * @return PermissionInfo
     */
    @Override
    public PermissionInfo detail(String id) {
        IdQueryWrapperBuilder<PermissionInfo> idQueryWrapperBuilder = new IdQueryWrapperBuilder<>(PermissionInfo.class, id);
        return this.indexHelper.searchById(idQueryWrapperBuilder.build());
    }

    /**
     * 保存
     *
     * @param permissionInfo 信息
     */
    @Override
    public void save(PermissionInfo permissionInfo) {
        CountQueryWrapperBuilder<PermissionInfo> queryWrapperBuilder = new CountQueryWrapperBuilder<>(PermissionInfo.class);
        queryWrapperBuilder.matchStr(PermissionInfo::getSourcePlatformId,
                        permissionInfo.getSourcePlatformId(), MatchTypeEnum.TERM, BooleanClause.Occur.MUST)
                .matchStr(PermissionInfo::getTargetPlatformId,
                        permissionInfo.getTargetPlatformId(), MatchTypeEnum.TERM, BooleanClause.Occur.MUST);
        Long count = this.indexHelper.count(queryWrapperBuilder.build());
        if (count > 0) {
            throw new AppException(JdfSystemMsgEnum.PERMISSION_PLATFORM_CONFLICT);
        }
        this.indexHelper.addDocument(permissionInfo);
    }

    /**
     * 更新
     *
     * @param permissionInfo 参数
     */
    @Override
    public void update(PermissionInfo permissionInfo) {
        CountQueryWrapperBuilder<PermissionInfo> queryWrapperBuilder = new CountQueryWrapperBuilder<>(PermissionInfo.class);
        queryWrapperBuilder.matchStr(PermissionInfo::getSourcePlatformId,
                        permissionInfo.getSourcePlatformId(), MatchTypeEnum.TERM, BooleanClause.Occur.MUST)
                .matchStr(PermissionInfo::getTargetPlatformId,
                        permissionInfo.getTargetPlatformId(), MatchTypeEnum.TERM, BooleanClause.Occur.MUST)
                .matchStr(BaseEntity::getId, permissionInfo.getId(), MatchTypeEnum.TERM, BooleanClause.Occur.MUST_NOT);
        Long count = this.indexHelper.count(queryWrapperBuilder.build());
        if (count > 0) {
            throw new AppException(JdfSystemMsgEnum.PERMISSION_PLATFORM_CONFLICT);
        }
        this.indexHelper.updateDocumentById(permissionInfo);
    }

    /**
     * 删除
     *
     * @param ids 主键
     */
    @Override
    public void delete(List<String> ids) {
        if (CollUtil.isEmpty(ids)) {
            return;
        }
        DeleteByIdsWrapperBuilder<PermissionInfo> deleteByIdsWrapperBuilder = new DeleteByIdsWrapperBuilder<>(PermissionInfo.class, ids);
        this.indexHelper.deleteByIds(deleteByIdsWrapperBuilder.build());
    }

    private QueryWrapperBuilder<PermissionInfo> queryBuilder(PermissionInfo permissionInfo) {
        QueryWrapperBuilder<PermissionInfo> queryWrapperBuilder = new QueryWrapperBuilder<>(PermissionInfo.class);
        if (CharSequenceUtil.isNotEmpty(permissionInfo.getSourcePlatformId())) {
            queryWrapperBuilder.matchStr(PermissionInfo::getSourcePlatformId,
                    permissionInfo.getSourcePlatformId(), MatchTypeEnum.TERM, BooleanClause.Occur.MUST);
        }
        if (CharSequenceUtil.isNotEmpty(permissionInfo.getTargetPlatformId())) {
            queryWrapperBuilder.matchStr(PermissionInfo::getTargetPlatformId,
                    permissionInfo.getTargetPlatformId(), MatchTypeEnum.TERM, BooleanClause.Occur.MUST);
        }
        return queryWrapperBuilder;
    }
}
